﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.IO;

// Prototype Using
using Prototype.Dialogs;
using Prototype.Projects;

namespace Prototype
{
    public partial class PrototypeEditor : Form
    {
        protected List<Image> images = new List<Image>();
        IniFile PreferencesFile = new IniFile();

        public PrototypeEditor()
        {
            InitializeComponent();
        }

        // Event called when the editor is first loaded
        private void PrototypeEditor_Load(object sender, EventArgs e)
        {
            // Create ini file if need be.
            PreferencesFile.CheckIni();

            // Recent Projects Check
            Recent RecentProjects = new Recent();

            if (Convert.ToBoolean(PreferencesFile.IniReadValue("STARTUP", "load_splash")))
            {
                // Display Splash Screen
                showSplashDialog();
            }
        }

        // Displays welcome screen splash dialog
        protected void showSplashDialog()
        {
            // Open and show the splash dialog
            SplashDialog splashDialog = new SplashDialog();
            splashDialog.ShowDialog();

            // If the user clicked new project button
            if (splashDialog.Result == SplashDialogResult.NEWPROJECT)
            {
                showNewProjectDialog();
            }
        }

        // Shows the new project dialog and gets its result
        protected void showNewProjectDialog()
        {
            // Open and show the New Project dialog
            NewProject newProjDialog = new NewProject();
            DialogResult result = newProjDialog.ShowDialog();

            // If the new project dialog was closed properly (using finish button)
            if (result == DialogResult.OK)
            {
                // Set the title of the editor window
                Text = "PrototypeEditor: " + newProjDialog.ProjectInfo.Name;
                foreach (string pkg in newProjDialog.ProjectInfo.Packages)
                {
                    MessageBox.Show("Package Imported: " + pkg);
                }

                enableEditor(true);
            }
        }

        // Used to enable/disable controls if project is open or not
        protected void enableEditor(bool enable)
        {
            // Let the user begin working on the project
            toolStripLibrary.Enabled = true;
        }

        // "File -> New Project" has been clicked
        private void menuMainFileNew_Click(object sender, EventArgs e)
        {
            showNewProjectDialog();
        }

        // "File -> Open Project" has been clicked
        private void menuMainFileOpen_Click(object sender, EventArgs e)
        {

        }

        // "File -> Save" has been clicked
        private void menuMainFileSave_Click(object sender, EventArgs e)
        {

        }

        // "File -> Save As" has been clicked
        private void menuMainFileSaveAs_Click(object sender, EventArgs e)
        {

        }

        // "File -> Close Project" has been clicked
        private void menuMainFileClose_Click(object sender, EventArgs e)
        {
            // Reset the editors window title
            Text = "PrototypeEditor";
            // Disable the editor until a project has been opened/created
            toolStripLibrary.Enabled = false;
        }

        // "File -> Exit" has been clicked
        private void menuMainFileExit_Click(object sender, EventArgs e)
        {
            askBeforeQuit();
        }

        // "Edit -> Preferences" has been clicked
        private void menuMainEditPreferences_Click(object sender, EventArgs e)
        {
            Preferences preferencesDlg = new Preferences();
            preferencesDlg.ShowDialog();
        }

        // Asks the user "Are you sure?" before quiting editor
        protected void askBeforeQuit()
        {
            if (MessageBox.Show(Globals.EXIT_MESSAGE, Globals.EXIT_TITLE, MessageBoxButtons.YesNo) == DialogResult.Yes)
            {
                Close();
            }
        }

        // Import asset toolbar button on library was clicked
        private void btnLibraryAdd_Click(object sender, EventArgs e)
        {
            // Import asset, with All files set as filter
            importAsset(1);
        }

        // Shows open dialog and imports an asset
        protected void importAsset(int type)
        {
            // 1 = all files, 2 = images, 3 = scripts, 4 = sounds
            fileDialogImportAsset.FilterIndex = type;

            if (fileDialogImportAsset.ShowDialog() == DialogResult.OK)
            {
                string filename = fileDialogImportAsset.FileName;
                if(getAssetType(filename) == Globals.ASSET_TYPE.IMAGE)
                {
                    importImage(filename);
                }
                else if (getAssetType(filename) == Globals.ASSET_TYPE.SCRIPT)
                {
                    importScript(filename);
                }
                else if (getAssetType(filename) == Globals.ASSET_TYPE.SOUND)
                {
                    importSound(filename);
                }
            }
        }

        // Get the type of asset from its extension
        protected Globals.ASSET_TYPE getAssetType(string filename)
        {
            Globals.ASSET_TYPE assetType = Globals.ASSET_TYPE.UNKNOWN;
            string ext = Path.GetExtension(filename);

            // Supported file types/extensions
            string[] imageExtensions = { ".png", ".jpg", ".bmp", ".gif", ".tga" };
            string[] scriptExtensions = { ".lua", ".py", ".js" };
            string[] soundExtensions = { ".ogg", ".mp3", ".wav" };

            // Check extension against image file extensions
            foreach (string fileType in imageExtensions)
            {
                if (ext.Equals(fileType))
                {
                    assetType = Globals.ASSET_TYPE.IMAGE;
                }
            }

            // Check extension against script file extensions
            foreach (string fileType in scriptExtensions)
            {
                if (ext.Equals(fileType))
                {
                    assetType =  Globals.ASSET_TYPE.SCRIPT;
                }
            }

            // Check extension against sound file extensions
            foreach (string fileType in soundExtensions)
            {
                if (ext.Equals(fileType))
                {
                    assetType =  Globals.ASSET_TYPE.SOUND;
                }
            }

            // Return the correct asset type
            return assetType;
        }

        // Imports an image into the projects library
        protected void importImage(string filename)
        {
            Bitmap img = new Bitmap(filename);
            images.Add(img);

            TreeNode node = treeView1.Nodes.Add(Path.GetFileName(filename));
            node.ImageIndex = node.SelectedImageIndex = 1;
            node.Name = filename;
        }

        // Imports a script into the projects library
        protected void importScript(string filename)
        {
            TreeNode node = treeView1.Nodes.Add(Path.GetFileName(filename));
            node.ImageIndex = node.SelectedImageIndex = 2;
            node.Name = filename;
        }

        // Imports a sound into the projects library
        protected void importSound(string filename)
        {
            TreeNode node = treeView1.Nodes.Add(Path.GetFileName(filename));
            node.ImageIndex = node.SelectedImageIndex = 3;
            node.Name = filename;
        }

        // When an asset in the library has been double clicked
        private void treeView1_NodeMouseDoubleClick(object sender, TreeNodeMouseClickEventArgs e)
        {
            TreeNode node = e.Node;
            if (node.ImageIndex == 1) // image
            {
                AssetViewerImage imgViewer = new AssetViewerImage();
                imgViewer.setImage(new Bitmap(node.Name), node.Name);
                imgViewer.Show();
            }
            else if (node.ImageIndex == 4) // Scene
            {
                tabControl1.TabPages.Add(node.Text);
            }
        }

        // "Add -> Scene" has been clicked
        private void menuMainAddScene_Click(object sender, EventArgs e)
        {
            TreeNode node = treeView1.Nodes.Add("NewScene");
            node.ImageIndex = node.SelectedImageIndex = 4;
            node.BeginEdit();
        }

        // "Add -> Import -> Image" has been clicked
        private void menuMainAddImportImage_Click(object sender, EventArgs e)
        {
            // Import asset with images set as default filter type
            importAsset(2);
        }
    }
}
